﻿latestLogTime = "";
var polling = 0;
var intervalID = -1;

jQuery(document).ready(function () {
    UlsViewer.StartLoggingCapture();
});

var UlsViewer = {
    
	StartLoggingCapture: function() {
	    UlsViewer.Debug('Starting to capture Uls logs');
        UlsViewer.PollForUlsLogs();
        intervalID = window.setInterval("UlsViewer.PollForUlsLogs()", 5000);
        $("#ulsviewer-start").hide();
        $("#ulsviewer-pause").show();	
	},
        
    GrabLatestUlsLogs: function() {
        UlsViewer.Debug('Getting the latest Uls logs');
        UlsViewer.PollForUlsLogs();
	},

    Pause: function() {
		clearInterval(intervalID);
        $("#ulsviewer-start").show();
        $("#ulsviewer-pause").hide();
	},

    Clear: function() {
        $("#uls-logs").html("");
	},

    PollForUlsLogs: function() {
        UlsViewer.Debug('checking whether to Poll ULS, polling is ' + polling + ' therefore I will poll? ' + (polling == 0));
        if (polling == 0) {
            polling = 1;
            try {
                jQuery.ajax({
                    type: "POST",
                    url: "/_layouts/UlsLogViewer/Uls/RefreshUlsHandler.ashx",
                    data: { LatestLogTime : latestLogTime},
                    success: function (data) {
                        UlsViewer.Debug(data);
                        UlsViewer.AddToAllLogs(data);
                        polling = 0;
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            }
            catch(err) {}
        }
	},

    AddToAllLogs: function(data) {
        UlsViewer.Debug('Polling the latest Uls logs');
        var logEntries = eval("(" + data + ")");
        latestLogTime = logEntries.LatestLogTimeAsString;
        UlsViewer.SendLogsToScreen(logEntries);
	},

    SendLogsToScreen: function(logEntries) {
        $.each(logEntries.LogEntries, function (i, logentry) {
            var message = logentry.Message;
            if (message.length > 50) {
                message = message.substring(0, 50) + "...";
            }
            UlsViewer.AppendHtml(UlsViewer.GetLogHTML('ulslog', logentry));
        });
        $('.scroll-pane').jScrollPane();
	},

    AppendHtml: function(html) {
        $("#uls-logs").append(html);
    },

    GetLogHTML: function(id, logentry) {
        var message = logentry.Message;
        var correlation = logentry.Correlation;
        if (correlation == "") {
            correlation = "no correlation id";
        }
        var html = "<li id='"+ id + logentry.Id + "' class='ui-widget-content'>"
                        + "<div class='log-container'>"
                        + "<div class='TimeStamp-container'>" + logentry.TimeStamp + "</div>"
                        + "<div class='Correlation-container'>" + logentry.Correlation + " </div>"
                        + "<div class='Level-container'>" + logentry.Level + "</div>"
                        //+ "<div class='Category-container'>" + logentry.Category + "</div>"
                        + "<div class='message-container'>" + message + "</div>"
                    + "</div>"
                    + "</li>";
        return html;
	},

    Debug: function (message) {
        var debug = UlsViewer.GetUrlParameterByName('debugjs');
        if (debug) {
            alert(message);
        }
    },

    GetUrlParameterByName: function (name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.search);
        if (results == null)
            return "";
        else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
};